package com.jyxd.web.controller.patient;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jyxd.web.data.common.Result;
import com.jyxd.web.data.dto.VitalSignsStatisticDataDTO;
import com.jyxd.web.data.patient.PatientNormalValue;
import com.jyxd.web.data.user.User;
import com.jyxd.web.service.assessment.basic.NurseRecordFormService;
import com.jyxd.web.service.patient.IPatientNormalValueService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import static com.jyxd.web.util.UUIDUtil.getUUID;

/**
 * <p>
 * 患者治疗目标 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-09-20
 */
@RestController
@RequestMapping("/patientNormalValue")
public class PatientNormalValueController {
    @Autowired
    private IPatientNormalValueService patientNormalValueService;
    @Autowired
    private NurseRecordFormService nurseRecordFormService;

    @RequestMapping("/save")
    public Result<?> save(@RequestBody PatientNormalValue patientNormalValue, HttpSession session) {
        if (StringUtils.isBlank(patientNormalValue.getId())) {
            patientNormalValue.setId(getUUID());
        }

        User user = (User) session.getAttribute("user");
        if (user != null && StringUtils.isNotEmpty(user.getTenantId())) {
            patientNormalValue.setTenantId(user.getTenantId());
        }

        patientNormalValueService.saveOrUpdateNormalValue(patientNormalValue);
        return Result.succeed("数据保存成功");
    }

    @RequestMapping("/queryList")
    public Result<?> queryList(@RequestBody(required = false) PatientNormalValue patientNormalValue, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null && StringUtils.isNotEmpty(user.getTenantId())) {
            patientNormalValue.setTenantId(user.getTenantId());
        }

        QueryWrapper<PatientNormalValue> wrapper = new QueryWrapper<>(patientNormalValue);
        List<PatientNormalValue> list = patientNormalValueService.list(wrapper);
        return Result.succeed(list);
    }

    @PostMapping("/delete/{id}")
    public Result<?> delete(@PathVariable("id") String id) {
        patientNormalValueService.removeById(id);
        return Result.succeed("删除成功");
    }

    /**
     * 患者观察项折线图统计数据
     * @param map
     * @return
     */
    @RequestMapping("/statistic")
    public Result<?> statistic(@RequestBody Map<String,Object> map){
        if(!map.containsKey("patientId")){
            return Result.failed("参数异常");
        }
        if(!map.containsKey("date")){
            map.put("date", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        List<VitalSignsStatisticDataDTO> list = nurseRecordFormService.statisticVitalSigns(map);
        return Result.succeed(list);
    }
}
